﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NumericalMethod.Properties;
using System.IO;

namespace NumericalMethod
{
    public partial class Notes : UserControl
    {        

        public Notes(NoteType nType)
        {
            InitializeComponent();
            InitDocument(nType);
        }

        private void InitDocument(NoteType nType)
        {
            // locals
            object noSave = false;
            object missingVal = System.Reflection.Missing.Value;
            object fileName = getResourceType(nType);

            // instance word app, doc
            Microsoft.Office.Interop.Word._Application wordApp =
            new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible = false; // hides MS Word

            Microsoft.Office.Interop.Word._Document wordDoc;

            // open the document
            wordDoc = wordApp.Documents.Open(ref fileName, ref missingVal, ref missingVal,
            ref missingVal, ref missingVal, ref missingVal, ref missingVal, ref missingVal,
            ref missingVal, ref missingVal, ref missingVal, ref missingVal, ref missingVal,
            ref missingVal, ref missingVal, ref missingVal);

            // select the entire document and copy it
            wordDoc.Select();
            wordApp.Selection.Copy();

            // clear the target richtextbox control
            richTextBox1.Text = string.Empty;

            // paste the word doc into the richtextbox control
            // - this may not come out perfect; table formatting for example may not
            // survive the operation intact or hyperlinks might be altered (e.g., an
            // email address will show up with mailto added...)
            richTextBox1.Paste();
            richTextBox1.ReadOnly = true;
            // clean up
            wordDoc.Close(ref noSave, ref missingVal, ref missingVal);
            wordApp.Quit(ref noSave, ref missingVal, ref missingVal);
            File.Delete( (string)fileName );
        }

        private string getResourceType(NoteType nType)
        {
           byte[] file = {0};
           string filePath = System.IO.Path.GetTempPath() + "nmc.dat";
           switch ( nType )
           {
              case NoteType.BinaryOperation:
                 file = Resources._3_Chapter_1;
                 break;
              case NoteType.FloatingPoint:
                 file = Resources._3_Chapter_1;
                 break;
              case NoteType.ConvertingNumbers:
                 file = Resources._3_Chapter_1;
                 break;
              case NoteType.ErrorForm:
                 file = Resources._4_Chapter_2;
                 break;
              case NoteType.NonLinearSystem:
                 file = Resources._5_Chapter_3;
                 break;
              case NoteType.LinearSystemForm:
                 file = Resources._6_Chapter_4;
                 break;
              case NoteType.InterpolationForm:
                 file = Resources._7_Chapter_5;
                 break;
              case NoteType.CurveFitting:
                 file = Resources._8_Chapter_6;
                 break;
           }
           //using System.IO;
           using ( BinaryWriter binWriter =
               new BinaryWriter( File.Open( filePath, FileMode.Create ) ) )
           {
              try
              {
                 binWriter.Write( file );
              }
              catch ( Exception )
              {
                 
                 throw;
              }
           }
           return filePath;            
        }
    }

    public enum NoteType
    {
        BinaryOperation,
        FloatingPoint,
        ConvertingNumbers,
        ErrorForm,
        NonLinearSystem,
        LinearSystemForm,
        InterpolationForm,
        CurveFitting
    }
}
